package com.ebelter.nb.ui.activitys;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.models.beans.UpdateVersionBean;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.nb.R;
import com.ebelter.nb.model.http.NbUpdateService;
import com.ebelter.nb.model.http.request.NbNetUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class AboutActivity extends NBBaseActivity implements View.OnClickListener {
    public static final String TAG = "AboutActivity";
    private ImageView top_left_iv;
    private TextView version_tv;
    private RelativeLayout version_rl;
    private TextView version_desc_tv1;
    private TextView version_desc_tv2;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_about;
    }

    @Override
    protected void initView() {
        super.initView();
        EventBus.getDefault().register(this);
        FV();
        setListener();
    }

    private void setListener() {
        top_left_iv.setOnClickListener(this);
    }

    private void FV() {
        top_left_iv=findViewById(R.id.top_left_iv);

        TextView title_tv = findViewById(R.id.m_title_tv);
        TextView rightTitle_tv = findViewById(R.id.m_righttitle_tv);
        ViewUtils.setTextViewStr(title_tv,"关于");
        ViewUtils.hideView(rightTitle_tv);
        version_tv = findViewById(R.id.version_tv);
        ViewUtils.setTextViewStr(version_tv,"Version:"+AppUtils.getVerName(this));
        version_rl = findViewById(R.id.version_desc_rl);
        version_rl.setOnClickListener(this);
        version_desc_tv1 = findViewById(R.id.version_desc_tv1);
        version_desc_tv2 = findViewById(R.id.version_desc_tv2);
    }

    UpdateVersionBean mUpdateVersionBean;
    private String haveDownedStr;
    @Override
    protected void initData() {
        haveDownedStr=getString_(R.string.yxz_d);
        getNetVersion();

    }

    @Override
    public void onClick(View v) {
        int vid=v.getId();
        if (vid==R.id.top_left_iv){
            finish();
        }else if (vid==R.id.version_desc_rl){
            showUpdateDialog(this,mUpdateVersionBean);
        }

    }
    private TextTipDialog dialog;
    private void showUpdateDialog(Activity activity,UpdateVersionBean mUpdateVersionBean) {
        if (activity==null||activity.isFinishing()||activity.isDestroyed()||mUpdateVersionBean==null){
            return;
        }

        if (dialog!=null){
            dialog.cancel();
            dialog=null;
        }

        dialog=new TextTipDialog(activity, StringUtils.getResStr(R.string.New_version_is_available_touch_to_update));
        dialog.isPingBiBack=true;
        dialog.show();
        // dialog.setNativebtVisibility(View.GONE);
        dialog.setPositive_btBg(R.drawable.sp_s0176ff_r6);
        dialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }
            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
                update();
            }
        });
    }

    //更新按钮
    private void update() {
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        if (mUpdateVersionBean==null){
            return;
        }

        Intent updateIntent=new Intent(this,NbUpdateService.class);
        updateIntent.putExtra(NbUpdateService.DownloadSer,mUpdateVersionBean);
        startService(updateIntent);

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, "AboutActivity--onEventMainThread接收到CommonEventBus信息--" + event.toString());

        float p = (float) event.getContent();

        ToastUtil.show(haveDownedStr+" : "+NumUtils.numBaoLiuWei1(p*100)+"%");


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void getNetVersion() {
        if (NetUtils.available()){
            final int verCode= AppUtils.getVersionCode(this);//获取当前版本号
            LogUtils.i(TAG,"verCode---" + verCode);
            NbNetUtils.getInstance().getUpdateVersionJson(this, new HttpResponse<UpdateVersionBean>() {
                @Override
                public void result(boolean isRequestSuccess, String requestDes, UpdateVersionBean updateVersionBean, String s) {
                    LogUtils.i(TAG,"s---"+s);
                    if (isRequestSuccess&&updateVersionBean!=null){
                        ViewUtils.displayView(version_rl);
                        LogUtils.i(TAG,"服务器上的json--"+updateVersionBean.toString());
                        if (NumUtils.string2Int(updateVersionBean.getVerCode())>verCode){

                            mUpdateVersionBean=updateVersionBean;
                            ViewUtils.goneView(version_desc_tv2);
                            String s1=getString_(R.string.djgx_)+" : "+updateVersionBean.getFileName();
                            ViewUtils.setTextViewStr(version_desc_tv1,s1);

                        }else {
                            ViewUtils.setTextViewStr(version_desc_tv1,getString_(R.string.dqbb));
                            ViewUtils.setTextViewStr(version_desc_tv2,getString_(R.string.Latest));
                        }
                    }
                }
            });
        }
    }
}
